<!DOCTYPE html>
<html>
<head>
    <title>DeepSeek 本地聊天</title>
    <style>
        body { 
            font-family: Arial, sans-serif; 
            max-width: 1200px; 
            margin: 0 auto; 
            padding: 20px; 
            background-color: #f8f9fa;
        }
        .chat-container { 
            border: 1px solid #ddd; 
            padding: 20px; 
            height: 70vh; 
            min-height: 600px;
            overflow-y: auto; 
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .message { 
            margin: 15px 0; 
            padding: 15px; 
            border-radius: 10px; 
            max-width: 80%;
            word-wrap: break-word;
            line-height: 1.5;
        }
        .user { 
            background-color: #e3f2fd; 
            text-align: right; 
            margin-left: auto;
        }
        .bot { 
            background-color: #f5f5f5; 
            margin-right: auto;
        }
        .loading { 
            background-color: #fff3cd; 
            color: #856404; 
            font-style: italic; 
        }
        .input-container {
            margin-top: 20px;
            display: flex;
            gap: 10px;
            align-items: center;
        }
        input[type="text"] { 
            flex: 1;
            padding: 15px; 
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 16px;
        }
        button { 
            padding: 15px 25px; 
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        button:disabled { 
            opacity: 0.6; 
            cursor: not-allowed; 
        }
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }
    </style>
</head>
<body>
    <h1>DeepSeek 本地聊天</h1>
    <div id="chat" class="chat-container"></div>
    <div class="input-container">
        <input type="text" id="question" placeholder="输入你的问题...">
        <button onclick="askQuestion()">发送</button>
    </div>

    <script>
        function askQuestion() {
            const questionInput = document.getElementById('question');
            const sendButton = document.querySelector('button');
            const question = questionInput.value.trim();
            
            if (!question) return;
            
            // 禁用输入和按钮
            questionInput.disabled = true;
            sendButton.disabled = true;
            sendButton.textContent = '发送中...';
            
            // 添加用户消息
            addMessage('user', question);
            questionInput.value = '';
            
            // 添加加载状态
            const loadingId = addMessage('bot loading', '正在思考中...');
            
            // 调用后端
            fetch('/ask', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ question: question })
            })
            .then(response => response.json())
            .then(data => {
                // 移除加载状态
                removeMessage(loadingId);
                
                if (data.error) {
                    addMessage('bot', '错误：' + data.error);
                } else {
                    addMessage('bot', data.answer);
                }
            })
            .catch(error => {
                // 移除加载状态
                removeMessage(loadingId);
                addMessage('bot', '请求失败：' + error);
            })
            .finally(() => {
                // 重新启用输入和按钮
                questionInput.disabled = false;
                sendButton.disabled = false;
                sendButton.textContent = '发送';
                questionInput.focus();
            });
        }
        
        function addMessage(sender, text) {
            const chat = document.getElementById('chat');
            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${sender}`;
            messageDiv.textContent = text;
            messageDiv.id = 'msg_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9);
            chat.appendChild(messageDiv);
            chat.scrollTop = chat.scrollHeight;
            return messageDiv.id;
        }
        
        function removeMessage(messageId) {
            const messageDiv = document.getElementById(messageId);
            if (messageDiv) {
                messageDiv.remove();
            }
        }
        
        // 按回车发送消息
        document.getElementById('question').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                askQuestion();
            }
        });
    </script>
</body>
</html>